<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { IpcRenderer } from 'electron'

defineProps<{ msg: string }>()

function notification () {
  if (window.process) {
    const ipcRenderer: IpcRenderer = require('electron').ipcRenderer
    setTimeout(() => {
      // ipcRenderer.send('checkForUpdate')
    }, 3000)

    ipcRenderer.on('update', (_: any, data: any) => {
      console.log(data)
    })

    ipcRenderer.on('logVersion', (_: any, data: any) => {
      console.log('logVersion :', data)
    })

    ipcRenderer.on('isUpdateNow', (_: any, data: any) => {
      console.log(data)
      setTimeout(() => {
        ipcRenderer.send('isUpdateNow')
      }, 3000);
    })

    ipcRenderer.on('downloadProgress', (_: any, data: any) => {
      console.log(data.percent)
    })
  }
}

onMounted(() => {
  if (window.process) notification()
  ipcRenderer?.on('serializefailed', () => {
    console.log('====================')
  })
  console.log('create table if not exists student')
  ipcRenderer?.send('createTable', 'create table if not exists student')
  setTimeout(() => {
    const noteSql = 'insert into student (name, email, phone) values ("tom", "zsan@full.com", 18122234016);'
    // ipcRenderer?.send('deleteState', 'delete from student where id = 2;')
    ipcRenderer?.on('findInState', (_, sql, data) => {
      console.log(sql)
      if (data.length) {
        console.log(data)
      } else {
        ipcRenderer?.send('insertState', noteSql)
      }
    })
    ipcRenderer?.send('selectQuery', 'select * from student where id = 1;')
  }, 2000)
})

const count = ref(0)
</script>

<template>
  <h1>{{ msg }}</h1>

  <div class="card">
    <button type="button" @click="count++">count is {{ count }}</button>
    <p>
      Edit
      <code>components/HelloWorld.vue</code> to test HMR
    </p>
  </div>

  <p>
    Check out
    <a href="https://vuejs.org/guide/quick-start.html#local" target="_blank">create-vue</a>, the official Vue + Vite
    starter
  </p>
  <p>
    Install
    <a href="https://github.com/vuejs/language-tools" target="_blank">Volar</a>
    in your IDE for a better DX
  </p>
  <p class="read-the-docs">Click on the Vite and Vue logos to learn more</p>
</template>

<style scoped>
.read-the-docs {
  color: #888;
}
</style>
